package com.bailan.foreat.controller;


import com.bailan.foreat.entity.Address;
import com.bailan.foreat.entity.User;
import com.bailan.foreat.service.AddressService;
import com.bailan.foreat.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ste
 * @since 2023-02-21 07:50:43
 */
@Controller
@RequestMapping("/address")
public class AddressController {
    @Autowired
    AddressService addressService;

    @GetMapping("all")
    @ResponseBody
    public List<Address> all(HttpServletRequest request){

        User currentUser = UserUtils.getCurrentUser(request);

        List<Address> addressList = addressService.getAll(currentUser.getId());

        return addressList;
    }



    @RequestMapping("add")
    @ResponseBody
    public void add(Address address,HttpServletRequest request){

        User currentUser = UserUtils.getCurrentUser(request);

        addressService.addAddress(currentUser.getId(),address);

    }

    @RequestMapping("toSettingAddress")
    public String toSettingAddress(Integer addressId, Model model) {
        model.addAttribute("addressId", addressId);
        return "addresssetting";
    }

    @RequestMapping("selectById")
    @ResponseBody
    public Address selectById(Integer addressId) {
        Address address = addressService.selectById(addressId);
        return address;
    }


    @RequestMapping("update")
    @ResponseBody
    public void update(Address address) {

        Address addressById = addressService.getById(address.getId());
        if ("".equals(address.getProvince()))
            address.setProvince(addressById.getProvince());
        if ("".equals(address.getCity()))
            address.setCity(addressById.getCity());
        if ("".equals(address.getArea()))
            address.setArea(addressById.getArea());
        if ("".equals(address.getDetailAddress()) || address.getDetailAddress() == null)
            address.setDetailAddress(addressById.getDetailAddress());
        if ("".equals(address.getTel()) || address.getTel() == null)
            address.setTel(addressById.getTel());
        if ("".equals(address.getReceiver()) || address.getReceiver() == null)
            address.setReceiver(addressById.getReceiver());
        if ("".equals(address.getEmailCode()) || address.getEmailCode() == null)
            address.setEmailCode(addressById.getEmailCode());

        address.setUserId(addressById.getUserId());
        address.setStoreId(addressById.getStoreId());
        address.setStatus(addressById.getStatus());

        addressService.updateAddress(address);

    }


    @RequestMapping("delete")
    @ResponseBody
    public void delete(Integer addressId) {
        addressService.deleteById(addressId);
    }
}

